const db=wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav:["男频","女频"],
    curNum: 0,
    mtopimages:[
      {rotationChart:'../../images/mtop1.png'},
      {rotationChart:'../../images/mtop2.png'}
    ],
    wtopimages:[
      {rotationChart:'../../images/wtop1.png'},
      {rotationChart:'../../images/wtop2.png'}
    ],
    mNovels:[
      
    ],
    wNovels:[

    ]
  },

  // 跳转搜索页面
  goSearch:function(){
    wx.navigateTo({
      url: '../../pages/search/search',
    })
  },

  // 顶部导航栏
  tabnav: function (ev) {
    let index = ev.currentTarget.dataset.navindex
    this.setData({
      curNum: index,  
    })
  },

  onLoad: function () {
    this.getmToplist()
    this.getwToplist()
    this.getmNovels()
    this.getwNovels()
  },

  // 男频轮播图
  getmToplist(){
    db.collection('rotationChart').where({sex:'0'})
    .get().then(res =>{
      // console.log('男频轮播图列表',res)
      if (res.data && res.data.length > 0) {
        this.setData({mtopimages : res.data})
      }})
  },

  // 女频轮播图
  getwToplist(){
    db.collection('rotationChart').where({sex:'1'})
      .get().then(res =>{
        // console.log('女频轮播图列表',res)
        if (res.data && res.data.length > 0) {
          this.setData({wtopimages : res.data})
        }
      })
  },

  //跳转小说列表
  goNovel(e){
    wx.navigateTo({
      url: '../novels/novels?id='+e.currentTarget.dataset.id,
    })
  },

  //跳转到新书
  gonbook(n){
    let nbook=[]
    nbook.push(n.currentTarget.dataset.channel)
    nbook.push('新书')
    wx.navigateTo({
      url: '../index/nbook/nbook?nbook='+nbook,
    })
  },

  // 跳转到热门
  gohot(h){
    let hot=[]
    hot.push(h.currentTarget.dataset.channel)
    hot.push('热门')
    wx.navigateTo({
      url: '../index/hot/hot?hot='+hot,
    })
  },

  // 跳转到精品
  goartistic(a){
    let artistic=[]
    artistic.push(a.currentTarget.dataset.channel)
    artistic.push('精品')
    wx.navigateTo({
      url: '../index/artistic/artistic?artistic='+artistic,
    })
  },

  // 跳转到完本
  goebook(end){
    let ebook=[]
    ebook.push(end.currentTarget.dataset.channel)
    ebook.push('已完结')
    wx.navigateTo({
      url: '../index/ebook/ebook?ebook='+ebook,
    })
  },

  // 男频书籍列表
  getmNovels(){
    let channel ='男频'
    wx.cloud.callFunction({
      name:"getiNovels",
      data: {
        channel: channel,
      },
    })
    .then(res =>{
      // console.log('男频小说列表',res)
      this.setData({
        mNovels : res.result.list,
      })
    })
  },

  // 女频小说列表
  getwNovels(){
    let channel ='女频'
    wx.cloud.callFunction({
      name:"getiNovels",
      data: {
        channel: channel,
      },
    })
    .then(res =>{
      // console.log('女频小说列表',res)
      this.setData({
        wNovels : res.result.list,
      })
    })
  },

  onShow() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getmToplist()
    this.getwToplist()
    this.getmNovels()
    this.getwNovels()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    wx.showToast({
      icon: 'none',
      title: '没有更多数据了',
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})